package com.lwx.es.model;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.codec.binary.StringUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

/**
 * portal product model
 * 
 * @author yongqi.wu
 */
public class ProductEsModel extends ESModelImpl implements IESModel{

    private String id;
    private String model;
    private String productName;
    private Date createTime;
    private String categoryId;
    private String countryCode;
    private String languageCode;


    public ProductEsModel() {}


    public ProductEsModel(String id, String model, String productName, Date createTime,
            String categoryId) {
        this.id = id;
        this.model = model;
        this.productName = productName;
        this.createTime = createTime;
        this.categoryId = categoryId;
    }


    @Override
    public String getDocId() {
        return this.id;
    }

	@Override
    public String toString() {
        return String.format("GameEsModel{id=%s, model=%s, productName=%s, createTime=%s}", id,
                model, productName, createTime);
    }

    @Override
    public String getJson() throws IOException {

        XContentBuilder builder = XContentFactory.jsonBuilder().startObject();

        if (this.id != null) {
            builder.field(ProductESFieldEnum.ID.getValue(), this.id);
        } else {
            throw new RuntimeException("Validation Failed: id " + this.id);
        }
        if (null != this.model) {
            builder.field(ProductESFieldEnum.MODEL.getValue(), this.model);
        }
        if (null != this.productName) {
            builder.field(ProductESFieldEnum.PRODUCTNAME.getValue(), this.productName);
        }
        if (null != this.createTime) {
            builder.field(ProductESFieldEnum.CREATETIME.getValue(), this.createTime);
        }
        if (null != this.categoryId) {
            builder.field(ProductESFieldEnum.CATEGORYID.getValue(), this.categoryId);
        }
        if (null != this.countryCode) {
            builder.field(ProductESFieldEnum.COUNTRYCODE.getValue(), this.countryCode);
        }
        if (null != this.languageCode) {
            builder.field(ProductESFieldEnum.LANGUAGECODE.getValue(), this.languageCode);
        }

        builder.endObject();
        return builder.string();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }
    
    public String getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public String getLanguageCode() {
		return languageCode;
	}

	public void setLanguageCode(String languageCode) {
		this.languageCode = languageCode;
	}


}
